package Frame.shopSet;

import FCRendering.FCRMaster;
import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import VWorldGameStudio.FCItem;
import VWorldGameStudio.events.ShopEvent;
import io.reactivex.schedulers.Schedulers;
import lombok.Setter;

import java.util.Vector;
import java.util.concurrent.TimeUnit;

import static FCRendering.FCRMaster.*;

public class FunctionWantedShop extends AShopFuntion {
    private Vector<String> itemNames;

    private int showFristIndex = 0;

    @Setter
    private int endI = 0;

    private int wantedPrice = 0;
    private String selectItemName = "null";
    private int page = 0;//0是首页， 1是查看boss信息

    private String detailText = null;
    private int showDetalIndex = 0;
    private Runnable runText = null;

    public FunctionWantedShop() {
        buildShopWindow();
    }

    public void buildShopWindow() {
        itemNames = ((ShopEvent)fcv.getPlayingEvent()).getItemid();
        setEndI(Math.min(itemNames.size(), showFristIndex + 7));
        resetUI();
    }

    private void resetUI() {
        FCRValues.resetUnifySayUIEntity();
        if (page == 0) {
            FCRValues.leftWindowUI[0].leftText = "赏金任务";
            FCRValues.leftWindowUI[1].leftText = "领取赏金";
            FCRValues.rightWindowUI[6].rightText = "退出";
        } else if (page == 1) {
            int m = 0;
            for (int i = showFristIndex; i < endI; i++) {
                String name = itemNames.get(i);
                FCItem item = itemLib.getItemOfID(name);
                FCRValues.leftWindowUI[m].leftText = item.getChName();
                String relatedmonsterName = itemLib.getItemOfID(itemNames.get(i)).getRelatedMonster();
                if (relatedmonsterName != null && gameSave.getHasGetRewardMonsterList().contains(relatedmonsterName)) {
                    FCRValues.leftWindowUI[m].rightText = "已击杀";
                } else {

                    FCRValues.leftWindowUI[m].rightText = "赏金" + item.getWantedPrice() + "G";
                }

                m++;
            }
            FCRValues.rightWindowUI[4].rightText = "上一页";
            FCRValues.rightWindowUI[5].rightText = "下一页";
            FCRValues.rightWindowUI[6].rightText = "返回";
        }
    }

    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {
        if (entity.leftText.length() > 0) {
            if (page == 0) {
                if (i == 0) {
                    page = 1;
                    showFristIndex = 0;
                    setEndI(Math.min(itemNames.size(), showFristIndex + 7));
                    resetUI();
                } else if (i == 1) {
                    //TODO 领取赏金
                    boolean getMoney = false;
                    int money = 0;
                    for (int n = 0; n < itemNames.size(); n++) {
                        String relatedmonsterName = itemLib.getItemOfID(itemNames.get(n)).getRelatedMonster();
                        if (relatedmonsterName == null) {
                            continue;
                        }
                        if (gameSave.getFcMonsterKillRecordMap().containsKey(relatedmonsterName)) {
                            if (!gameSave.getHasGetRewardMonsterList().contains(relatedmonsterName)) {
                                player1.setMoney(player1.getMoney() + itemLib.getItemOfID(itemNames.get(n)).getWantedPrice());
                                money += itemLib.getItemOfID(itemNames.get(n)).getWantedPrice();
                                gameSave.getHasGetRewardMonsterList().add(relatedmonsterName);
                                getMoney = true;
                            }
                        }
                    }
                    if (getMoney) {
                        FCRValues.rightWindowUI[0].leftText = "已经领取完所有赏金" + money + "G";
                    } else {
                        FCRValues.rightWindowUI[0].leftText = "没有新的赏金boss被击杀";
                    }
                }
            } else if (page == 1) {
                selectItemName = itemNames.get(showFristIndex + i);
                FCItem item = itemLib.getItemOfID(selectItemName);
                wantedPrice = item.getWantedPrice();
                resetUI();
                FCRValues.unifyShopSayShowImg = item.getPngPng();
                detailText = item.getDepict();
                showDetalIndex = 0;
                showText(1);
                if (runText == null) {
                    runText = showRightDetailTextRun;
                    runText.run();
                }
            }
        }
    }

    private void showText(int mode) {
        if (detailText != null) {
            //TODO 将FCRValues.rightWindowUI[0].right显示3行出来，每行5个字
            int end = Math.min(showDetalIndex + 7, detailText.length());
            FCRValues.rightWindowUI[0].rightText = detailText.substring(showDetalIndex, end);
            if (end < detailText.length()) {
                end = Math.min(showDetalIndex + 14, detailText.length());
                FCRValues.rightWindowUI[1].rightText = detailText.substring(showDetalIndex + 7, end);
                if (end < detailText.length()) {
                    end = Math.min(showDetalIndex + 21, detailText.length());
                    FCRValues.rightWindowUI[2].rightText = detailText.substring(showDetalIndex + 14, end);
                    if (end < detailText.length()) {
                        end = Math.min(showDetalIndex + 28, detailText.length());
                        FCRValues.rightWindowUI[3].rightText = detailText.substring(showDetalIndex + 21, end);
                    } else {
                        FCRValues.rightWindowUI[3].rightText = "";
                    }
                } else {
                    FCRValues.rightWindowUI[2].rightText = "";
                    FCRValues.rightWindowUI[3].rightText = "";
                }
            } else {
                FCRValues.rightWindowUI[1].rightText = "";
                FCRValues.rightWindowUI[2].rightText = "";
                FCRValues.rightWindowUI[3].rightText = "";
            }
            if (mode == 2) {
                if (end == detailText.length()) {
                    //结尾了
                    showDetalIndex = 0;
                } else {
                    showDetalIndex += 21;
                }
            }
        }
    }

    private Runnable showRightDetailTextRun = new Runnable() {
        @Override
        public void run() {
            if (fcv.isOpenSayShop()) {
                showText(2);
                Schedulers.io().scheduleDirect(showRightDetailTextRun, 3, TimeUnit.SECONDS);
            }
        }
    };

    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {
        switch (i) {
            case 4:
                if (page == 1) {
                    if (showFristIndex >= 7) {
                        showFristIndex -= 7;
                        setEndI(Math.min(itemNames.size(), showFristIndex + 7));
                        resetUI();
                    }
                }
                break;
            case 5:
                if (page == 1) {
                    if (endI < itemNames.size()) {
                        showFristIndex += 7;
                        setEndI(Math.min(itemNames.size(), showFristIndex + 7));
                        resetUI();
                    }
                }
                break;
            case 6:
                if (page == 1) {
                    page = 0;
                    detailText = null;
                    resetUI();
                } else {
                    detailText = null;
                    fcv.setOpenSayShop(false);
                    FCRMaster.fcv.setOpenSay(false);
                    FCRMaster.fcv.setSayNpcName(null);
                    FCRMaster.fcv.setSayNpcTextNum(0);
                }
                break;
        }
    }
}
